package calculated

import (
	"fmt"
	"iot/common"
	"iot/common/sys"
	"iot/plat/device"
)

func Start() {
	sys.Bus().Subscribe(common.MonitorPointChange, func(args ...interface{}) {
		if len(args) != 1 {
			panic(args)
		}
		v, ok := args[0].(device.SRtValue)
		if !ok {
			panic(args[0])
		}

		dev := device.GetDeviceMgr().DeviceByArgIndex(v.ArgIndex)
		if dev == nil {
			return
		}
		dev.SetRtArg(v)
	})

	sys.Bus().Subscribe(common.MonitorPeriod, func(args ...interface{}) {
		if len(args) != 1 {
			panic(args)
		}
		v, ok := args[0].(device.SRtValue)
		if !ok {
			panic(args[0])
		}

		dev := device.GetDeviceMgr().DeviceByArgIndex(v.ArgIndex)
		if dev == nil {
			return
		}
		dev.SetRtArg(v)
		fmt.Printf("rtvalue=%v\n", v)
	})
}
